package logic

import (
	"context"
	"gitee.com/Cauchy_AQ/vxopen/model/mongodb"
	"gitee.com/Cauchy_AQ/vxopen/rpc/transform/internal/svc"
	"gitee.com/Cauchy_AQ/vxopen/rpc/transform/transform"
	"github.com/zeromicro/go-zero/core/logx"
)

const (
	metadataPrefix = "gateway-"
)

type UserinfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserinfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserinfoLogic {
	return &UserinfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 可以选择将claims存在context中传过来
// 也可以选择针对不同的接口，将需要的字段作为请求信息Req传来
func (l *UserinfoLogic) Userinfo(in *transform.UserInfoReq) (*transform.UserInfoResp, error) {
	/* 一种metadata传输方式
	md, ok := metadata.FromIncomingContext(l.ctx)
	if !ok {
		return nil, errors.ErrorMedataFromIncomingContext
	}
	*/
	var err error
	var user *mongodb.User
	mongoClient := mongodb.GetMongoInstance(l.svcCtx.Config.ModelConfig)

	if len(in.Openid) > 0 {
		user, err = mongoClient.UserCol.FindOneByOpenId(context.Background(), in.Openid)
	} else {
		user, err = mongoClient.UserCol.FindOneByMobile(context.Background(), in.Mobile)
	}
	if err != nil {
		return nil, err
	}

	return &transform.UserInfoResp{
		Name:   user.Name,
		Avatar: user.Avatar,
		Desc:   user.Desc,
		Vx:     user.VX,
		Qq:     user.QQ,
		RoleInfo: &transform.RoleInfo{
			Name:       user.Role.Name,
			Permission: user.Role.Permission,
		},
	}, nil
}
